﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AD.DAL;
using AD.Model;

namespace AD.BLL
{
    public class KeyWordsBLL : BLLBase
    {
        public static List<KeyWordsMod> Query(string keywords, string engine)
        {
            return KeyWordsDAL.Query(keywords, engine);
        }
        public static int SumFilter(string filter)
        {
            return KeyWordsDAL.Sum(filter);
        }
        public static int Sum()
        {
            return SumFilter("");
        }
        public static List<KeyWordsMod> QueryByPage(string filter, int pageSize, int index, out int maxCount)
        {
            return KeyWordsDAL.QueryByPage(filter, pageSize, index, out maxCount);
        }

        public static int Insert(KeyWordsMod keyword)
        {
            return KeyWordsDAL.Insert(keyword);
        }

        public static int UpdateInfo(KeyWordsMod keyword)
        {
            return KeyWordsDAL.UpdateInfo(keyword);
        }

        public static int Delete(KeyWordsMod keyword)
        {
            return KeyWordsDAL.Delete(keyword);
        }

        public static int Delete(int autoId)
        {
            return KeyWordsDAL.Delete(autoId);
        }

        public static int UpdateAudit(KeyWordsMod keyword)
        {
            return UpdateAudit(keyword.Status, keyword.AutoID);
        }

        public static int UpdateAudit(KeyWordsStatus status, int id)
        {
            return KeyWordsDAL.UpdateAudit(status, id);
        }

        public static KeyWordsMod CheckKeyWords(KeyWordsMod keyword)
        {
            return KeyWordsDAL.CheckKeyWords(keyword);
        }

        public static IList<KeyWordsMod> QueryFilterToList(string filter)
        {
            return KeyWordsDAL.QueryFilterToList(filter);
        }

        public static IList<KeyWordsMod> QueryFilterByKeyWord(string keyword)
        {
            string filter = string.Format(" KeyWords='{0}'", keyword);
            return QueryFilterToList(filter);
        }


        public static IList<KeyWordsMod> QueryFilterByUserId(string userId)
        {
            string filter = string.Format(" UserId={0}", userId);
            return QueryFilterToList(filter);
        }
        public static IList<KeyWordsMod> QueryFilterByUserId(int userId)
        {
            string filter = string.Format(" UserId={0}", userId);
            return QueryFilterToList(filter);
        }
        public static KeyWordsMod QueryById(string Id)
        {
            IList<KeyWordsMod> tmp = QueryFilterById(Id);
            if (tmp != null && tmp.Count > 0)
                return tmp[0];
            return null;
        }
        public static KeyWordsMod QueryById(int Id)
        {
            IList<KeyWordsMod> tmp = QueryFilterById("" + Id);
            if (tmp != null && tmp.Count > 0)
                return tmp[0];
            return null;
        }
        public static IList<KeyWordsMod> QueryFilterById(string Id)
        {
            string filter = string.Format(" AutoID={0}", Id);
            return QueryFilterToList(filter);
        }

        public static DataTable QueryFilterToTable(string filter)
        {
            return KeyWordsDAL.QueryFilterToTable(filter);
        }

        public static int UpdateUse(string Id, out int errcode)
        {
            errcode = -1;
            IList<KeyWordsMod> tmp = QueryFilterById(Id);
            if (tmp != null && tmp.Count > 0)
            {
                if (Math.Abs(KeyWordsDAL.GetDay(DateTime.Now, tmp[0].UseUpdateDate)) >= 1)
                    return KeyWordsDAL.UpdateUse(!tmp[0].IsUse, Id);
                else errcode = -2;
            }
            return 0;
        }
    }
}